package principal;

import com.asprise.util.ocr.OCR;
import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;


public class ReconocedorEscritura implements IReconocedorFormas {

	@Override
	public int reconoceTrazo() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void setTrazo(Trazo t) {
		// TODO Auto-generated method stub

	}
	
	public Image getImage(Component component)
    {
                   if(component==null) {return null;}
                   int width = 912;
                   int height = 200;
                   BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
                   Graphics2D g = image.createGraphics();
                   component.paintAll(g);
                   g.dispose();
                   return image;
     }
 

    public String reconoceEscritura(Component lienzo) throws ImageFormatException, IOException
    {
        Image aux=getImage(lienzo);
        BufferedImage tnsImg = new BufferedImage(912,200, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics2D = tnsImg.createGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2D.drawImage(aux, 0, 0, 912, 200, null);
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("C:\\BPMNS/Mito2.jpg"));
        //creamos el "parseador" para guardar la imagen en formato JPG
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
        JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(tnsImg);

        //Asignamos la calidad con la que se va a guardar la imagen de 0-100
        param.setQuality(50, false);
        encoder.setJPEGEncodeParam(param);
        encoder.encode(tnsImg);
        out.close();
        BufferedImage image = ImageIO.read(new File("C:\\BPMNS/Mito2.jpg"));
        String s = new OCR().recognizeEverything(image);
        //System.out.println(s);
        return s;     
    }

}
